package demo.controller;

import demo.entity.UserDO;
import demo.model.PageQuery;
import demo.model.result.PageResult;
import demo.model.result.Response2Result;
import demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author meilin.huang
 * @date 2022-03-09 8:51 PM
 */
@Response2Result
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
//    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public PageResult<UserDO> listByQuery(PageQuery query) {
        return PageResult.withPageHelper(query, () -> userService.listAll());
    }

    @PostMapping("/users")
    public void create(@RequestBody UserDO user) {
        userService.insert(user);
    }

    @DeleteMapping("/users/{id}")
    public void delete(@PathVariable Long id) {
        userService.deleteById(id);
    }
}
